/**
 * 
 */
package com.johnny.xia.filetest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.johnny.xia.util.file.AppendToFile;

/**
 * @author johnnyxia
 *
 */
public class NginxLogCurrent2{
    
   static Logger log = LoggerFactory.getLogger(NginxLogCurrent2.class);
   
   static String patten = "\\d\\d/\\w{3}/201[6|7]:\\d\\d:\\d\\d:\\d\\d";
   static Pattern pattern = Pattern.compile(patten);
    
    public static void main(String[] args){
       
       String moth="201610";
        
       //日志主目录
       String mainPath = "C:\\mclog\\nginx_log\\"+moth+"\\";
       
       //待分析日志目录
       String file="nginx_oauth.log";
       
       //生成目标日志
       String targetFile="nginx_"+moth+".log";
        
       Set<String> set = readNginxLogByLine(mainPath+file);
       
       for(String str : set){
           AppendToFile.appendMethodA(mainPath+targetFile, str);
       }
       
       System.out.println(moth+" 共 "+set.size()/2 +" 次并发请求");
       
        
    }
    
    public static int isConcurrency(Date start,Date end){
       if((end.getTime() - start.getTime()) == 0){
           return 0;
       }else if(end.getTime() - start.getTime() == 1000){
           return 1;
       }else{
           return 2;
       }
    }
    
    public static Date str2date(String str){
        
        SimpleDateFormat format = new SimpleDateFormat("dd/MMM/yyyy:hh:mm:ss", Locale.ENGLISH);

        try{
            Date date = format.parse(str);
            return date;
        }catch (ParseException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    public static String regexpGetTime(String data){
        String time = "";
        Matcher matcher = pattern.matcher(data); 
        while (matcher.find()) {
            time = matcher.group(0);
        }
        
        return time;
    }
    
    
    /**
     * 间隔很近的请求
     */
    public static Set<String> readNginxLogByLine(String fileName){
        Set<String> set = new LinkedHashSet<>();

        File file = new File(fileName);
        BufferedReader reader = null;
        
        String previousLine = null;
        try{
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束
            int i=1;
            while ((tempString = reader.readLine()) != null){
                if(previousLine != null){
                    String time = regexpGetTime(previousLine);
                    Date start = str2date(time);
                    
                    String time2 = regexpGetTime(tempString);
                    Date end = str2date(time2);
                    
                    if(isConcurrency(start, end) ==0 
                                    && previousLine.contains("/oauth/microsoftLiveCallback") 
                                    && tempString.contains("/oauth/microsoftLiveCallback")){
                        
//                        log.info(time);
//                        log.info(time2);
                        
                        String t1[] = previousLine.split(" ");
                        String t2[] = tempString.split(" ");
                        
                        String preIp = t1[0].split(":")[1];
                        String curIp = t2[0].split(":")[1];
                        
                        
                        
                        //节点不一样
                        if(t1[t1.length-2].equals(t2[t2.length-2]) && !preIp.equals(curIp)){
                            
                            set.add(previousLine+"\r\n");
                            set.add(tempString+"\r\n");
                            String t="";
                            for(int n=0;n<i;n++){
                                t=t+" ";
                            }
                            set.add(t+"\r\n");
                            i++;
                        }
                       
                    }
                }
                previousLine = tempString;
            }
            reader.close();
        }catch (IOException e){
            e.printStackTrace();
        }finally{
            if (reader != null){
                try{
                    reader.close();
                }catch (IOException e1){}
            }
        }
        
        return set;
    }

}
